﻿import com.dmotiko.util.*
import com.dmotiko.general.*
import com.dmotiko.bolacua.*
import mx.utils.Delegate;

class MainPhotoBolacua
extends BaseClip {
	
	public static var FIRST_PHOTO_LOADED:String = "mainphotobolacua_firstphotoloaded";
	private static var FRAMES_TIMER:Number = 90;
	
	private var root:MainPhotoBolacua;
	private var mcSlideShow;
	private var mcSlideShow2;
	private var mcPhotoFooter;
	private var mcLoader:LoaderBolacua;
	private var mcl:MovieClipLoader;
	
	private var aSlides:Array;
	private var nCount:Number;
	private var bFirst:Boolean;
	private var lastSlide:MovieClip;
	
	private var timer:Function;
	private var timerController:MovieClip;
	
	function MainPhotoBolacua(){
		super();
		
		BolacuaSite.getApp().setMainPhotoBolacua(this);
		BolacuaSite.getApp().addEventListener( BolacuaSite.SECTION_CHANGED, Delegate.create(this, refreshContent));
		BolacuaSite.getApp().addEventListener( BolacuaSite.LANGUAGE_CHANGED, Delegate.create(this, refreshContent));
		
		root = this;
		bFirst = false;
		
		mcl = new MovieClipLoader();
		mcl.addListener(this);
		MovieEffects.timer();
		refreshContent();
	}
	
	public function refreshContent(){
		switch(BolacuaSite.getApp().getSection()){
			case BolacuaSite.HOME:
			aSlides = new Array( "mainPhoto1.swf", "mainPhoto2.swf", "mainPhoto3.swf", "mainPhoto4.swf", "mainPhoto5.swf" );		
			break;
			
			case BolacuaSite.ALOJAMIENTO:
			aSlides = new Array( "img/alojamiento1.jpg" );
			break;
			
			case BolacuaSite.COMO_LLEGAR:
			aSlides = new Array( "img/comoLlegar1.jpg" );
			break;
			
			case BolacuaSite.CONTACTO:
			aSlides = new Array( "img/contacto1.jpg" );
			break;
			
			case BolacuaSite.EVENTOS:
			aSlides = new Array( "img/eventos1.jpg" );
			break;
			
			case BolacuaSite.INFRAESTRUCTURA:
			aSlides = new Array( "img/infraestructura1.jpg" );
			break;
			
			case BolacuaSite.RESTAURANT:
			aSlides = new Array( "img/restaurant1.jpg" );
			break;
			
			case BolacuaSite.TARIFAS:
			aSlides = new Array( "img/tarifas1.jpg" );
			break;
		}
		startSlider();
	}
	
	private function startSlider():Void {
		nCount = 0;
		this.timerController.stopEffect();
		var nextSlide = (mcSlideShow == lastSlide) ? mcSlideShow2 : mcSlideShow;
		mcl.loadClip(aSlides[nCount], nextSlide);
	}
	
	private function onLoadStart(){
		if(!bFirst) {
			mcLoader.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, loadComplete) );	
		}
	}
	
	private function onLoadProgress(target, loaded, total){
		if(!bFirst) {
			var p = ((loaded/total)*100);
			p = Math.round(p);
			mcLoader.setPos(p);	
		}
	}
	
	private function onLoadInit(target){
		//chequeo en qué foto está para ver de qué color va el footer
		if(BolacuaSite.getApp().getSection() == BolacuaSite.HOME && nCount == 1){
			mcPhotoFooter.toWhite();
		} else {
			mcPhotoFooter.toBlack();
		}
		if(!bFirst) {
			bFirst = true;
			target._alpha = 0;
			lastSlide = target;
								
		} else {
			lastSlide.fadeOut( { rate: 7 });
			lastSlide = target;
			target._alpha = 0;
			target.fadeIn( { bind: this, listener: photoShowEnd, rate: 7 } );
		}
	}
	
	private function loadComplete(evnt, data){
		mcPhotoFooter.show();
		MovieEffects.fadeIn();
		MovieEffects.fadeOut();
		mcSlideShow._alpha = 0;
		mcSlideShow.fadeIn( { bind: this, listener: firstPhotoShowEnd, rate: 7 } );
	}
	
	private function firstPhotoShowEnd(){
		photoShowEnd();
	}
	private function photoShowEnd(){
		lastSlide.mcTitle.play();
		if( aSlides.length <= 1) return;
		else this.timerController.stopEffect();
				
		this.timerController = this.timer( { frames: FRAMES_TIMER, bind: this, listener: nextPhoto } );
	}
	
	private function nextPhoto(){
		var nextSlide = (mcSlideShow == lastSlide) ? mcSlideShow2 : mcSlideShow;
		nCount++;
		if(nCount > aSlides.length - 1) nCount = 0;
		mcl.loadClip(aSlides[nCount], nextSlide);
	}
	
}